/**
Audio.cpp
Title: Binge Audio Program File
Descr: Audio Class

Copyright (c) 2010, Brant Ryan Anderson
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <ORGANIZATION> nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/

#include "../../binge/core/Audio.h"

namespace binge
{

Audio::Audio()
{
    songNum = -1;
    musicPaused = false;
    playlist = false;
    disabled = false;
}

Audio::~Audio()
{
    this->Close();
}

bool
Audio::Init()
{
    return true;
}

void
Audio::Close(void)
{
    Music_Stop();

    //release all samples
    for (musicIter i = musicList.begin(); i != musicList.end(); ++i)
    {
        delete (*i);
        (*i) = 0;
    }
    for (effectIter i = effectList.begin(); i != effectList.end(); ++i)
    {
        delete (*i);
        (*i) = 0;
    }
}

bool
Audio::Music_Load(std::string filename, std::string name)
{
    if (filename.length() == 0 || name.length() == 0) return false;

    Music* music = new Music();
    music->SetName(name);
    music->SetFilename(filename.c_str());

    musicList.push_back(music);
    return true;
}

bool
Audio::Effect_Load(std::string filename, std::string name)
{
	if (filename.length() == 0 || name.length() == 0) return false;

	Effect* effect = new Effect();
        effect->SetName(name);
	if(!effect->sample.LoadFromFile(filename.c_str())) return false;

	effectList.push_back(effect);
	return true;
}

Music*
Audio::Music_Find(std::string name)
{
    Music* music = 0;
    for (musicIter i = musicList.begin(); i != musicList.end(); ++i)
    {
        if ((*i)->GetName() == name)
        {
            music = (*i);
            break;
        }
    }
    return music;
}

Effect*
Audio::Effect_Find(std::string name)
{
    Effect* effect = 0;
    for (effectIter i = effectList.begin(); i != effectList.end(); ++i)
    {
        if ((*i)->GetName() == name)
        {
            effect = (*i);
            break;
        }
    }
    return effect;
}

bool
Audio::Effect_Play(std::string name)
{
    if (!disabled)
    {
        Effect* effect = Effect_Find(name);

        if (effect != 0)
        {
            //effect found, play it
            effect->sound.SetBuffer(effect->sample);
            effect->sound.Play();
        } else { return false; }
    } else { return false; }

    return true;
}

bool
Audio::Music_Play(std::string name)
{
    if (!disabled)
    {
        playlist = false;
        Music_Stop();

        musicPaused = false;
        Music* music = Music_Find(name);
        if (musicSample.OpenFromFile(music->GetFilename().c_str()))
        {
            //music found, play it
            musicSample.SetLoop(true);
            musicSample.Play();
        } else { return false; }
    } else { return false; }

    return true;
}

bool
Audio::Music_ListPlay(void)
{
    if (!disabled)
    {
        if (songNum >= musicList.size()) songNum = 0;

        playlist = true;
        Music_Stop();

        musicPaused = false;
        if (musicSample.OpenFromFile(musicList[songNum]->GetFilename().c_str()))
        {
            //music found, play it
            musicSample.SetLoop(false);
            musicSample.Play();
        } else { return false; }
    } else { return false; }

    return true;
}

bool
Audio::Music_ListNext(void)
{
    if (!disabled)
    {
        ++songNum;
        if (songNum >= musicList.size()) songNum = 0;

        playlist = true;
        Music_Stop();

        musicPaused = false;
        if (musicSample.OpenFromFile(musicList[songNum]->GetFilename().c_str()))
        {
            //music found, play it
            musicSample.SetLoop(false);
            musicSample.Play();
        } else { return false; }
    } else { return false; }

    return true;
}

bool
Audio::Music_ListPrev(void)
{
    if (!disabled)
    {
        --songNum;
        if (songNum >= musicList.size()) songNum = musicList.size()-1;

        playlist = true;
        Music_Stop();

        musicPaused = false;
        if (musicSample.OpenFromFile(musicList[songNum]->GetFilename().c_str()))
        {
            //music found, play it
            musicSample.SetLoop(false);
            musicSample.Play();
        } else { return false; }
    } else { return false; }

    return true;
}

void
Audio::Music_Pause(void)
{
    if (!disabled)
    {
        musicSample.Pause();
        musicPaused = true;
    }
}

void
Audio::Music_Resume(void)
{
    if (!disabled)
    {
        musicSample.Play();
        musicPaused = false;
    }
}

void
Audio::Music_Stop(void)
{
    if (!disabled)
    {
        musicSample.Stop();
        musicPaused = false;
    }
}

bool
Audio::Music_IsPlaying(void)
{
    if (!disabled)
    {
        return (musicSample.GetStatus() == sf::Music::Playing);
    }

    return false;
}

} // namespace
